from apps.interface.node.baseViews import *


class NodeViewSet(BaseNodeViewSet):

    def get_queryset(self):
        _list = NodeDao().findAll().select_related('component')
        return _list

    '''根据界面配置获取节点'''
    @action(methods=['post'], detail=False)
    def getNodeByPageConfig(self, request):
        params = getRequestParams(request)
        nodes = Node.objects.filter(pageConfig=params.get('pageConfig'), is_delete=0).select_related('component').order_by('-sort')
        if not nodes.exists():
            Response(response_success('节点不存在'))
        nodeInfo = NodeModelSerializer(nodes, many=True).data
        return Response(response_success(nodeInfo))

    '''保存节点配置'''
    @action(methods=['post'], detail=False)
    def saveNodeConfig(self, request):
        params = getRequestParams(request)
        nodeList = params.get('nodeList')
        _node_ids = []
        for i in range(len(nodeList)):
            node = nodeList[i]
            # _advert_ids = []
            # for advert in node.get('advertList'):
            #     if advert.get('id'):
            #         print('----advert', advert)
            #         _advert = Advert.objects.get(id=advert.get('id'))
            #         serializers = AdvertBaseSerializer(_advert, data=advert, partial=True)
            #         if serializers.is_valid():
            #             serializers.save()
            #             _advert_ids.append(serializers.data.get('id'))
            #         else:
            #             print(serializers.errors)
            #             return Response(response_error(serializers.errors))
            #     else:
            #         serializers = AdvertBaseSerializer(data=advert)
            #         if serializers.is_valid():
            #             serializers.save()
            #             _advert_ids.append(serializers.data.get('id'))
            #         else:
            #             return Response(response_error(serializers.errors))
            # if node.get('adverts'):
            #     for advert1 in node.get('adverts'):
            #         isExist = False
            #         for advert2 in node.get('advertList'):
            #             if advert1 == advert2.get('id'):
            #                 isExist = True
            #         if not isExist:
            #             print('删除了', advert1)
            #             Advert.objects.filter(id=advert1).delete()

            # node['adverts'] = _advert_ids
            # print('sort', node.get('sort'))
            if node.get('sort') is None:
                print('sort2', node.get('sort'))
                if i > 0:
                    node['sort'] = nodeList[i-1].get('sort')
                else:
                    node['sort'] = 0
            # print('adverts', node.get('adverts'))
            if node.get('id'):
                print('node存在，直接更新')
                _node = Node.objects.get(id=node.get('id'))
                serializers = NodeBaseSerializer(_node, data=node, partial=True)
                if serializers.is_valid():
                    serializers.save()
                    _node_ids.append(node.get('id'))
                else:
                    print(serializers.errors)
                    return Response(response_error(serializers.errors))
            else:
                print('nodeId不存在，创建一个节点')
                serializers = NodeBaseSerializer(data=node)
                if serializers.is_valid():
                    serializers.save()
                    _node_ids.append(serializers.data.get('id'))
                else:
                    print(serializers.errors)
                    return Response(response_error(serializers.errors))
        print('_node_ids', _node_ids)
        if len(_node_ids)>0:
            Node.objects.filter(pageConfig=params.get('pageConfig'), is_delete=0).exclude(id__in=_node_ids).delete()
        else:
            Node.objects.filter(pageConfig=params.get('pageConfig'), is_delete=0).delete()
        return Response(response_success('操作成功'))